package com.Wheesper.www.WheesperServer.Model;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

import com.wheesper.www.WheesperServer.DAO.UserDAO;
import com.wheesper.www.WheesperServer.DAO.UserInfoDAO;
import com.wheesper.www.WheesperServer.Services.LogController;
import com.wheesper.www.WheesperServer.Utils.*;

//Help to Create User and Check if the User email
//had being used


public class UserCenter {
	private Set<Integer> _used_id;
	private Set<String> _used_email;
	private Map<String,Integer> _email2id;
	private Map<Integer,String> _id2email;
	private AtomicInteger _id_generator;
	private Map<String,User> _email2users;
	private Map<Integer,User> _id2users;
	
	//config this object at this line:
	private static UserCenter _instance = new UserCenter("UserCenter.data");
	
	public static UserCenter Instance() {
		return _instance;
	}
	
	private UserCenter(String file) {
		_id_generator = new AtomicInteger(0);
		_used_id = new HashSet<Integer>();
		_used_email = new HashSet<String>();
		_email2id = new HashMap<String,Integer>();
		_id2email = new HashMap<Integer,String>();
		_email2users = new HashMap<String,User>();
		_id2users = new HashMap<Integer,User>();
		//Read the reserved data from the disk.
		loadData(file);
	}
	
	private Integer generateID()
	{
		Integer ID = 0;
		while(_used_id.contains(ID))
			ID = _id_generator.incrementAndGet();
		_used_id.add(ID);
		return ID;
	}
	
	private User newUser()  {
		Integer ID = generateID();
		User user = new User(ID);
		return user;
	}
	
	public void loadData(String filePath) {
		//TODO:
	}
	
	public boolean checkEmail(String email) {
		return !_used_email.contains(email);
	}
	public boolean checkEmailExisted(String email) {
		return _used_email.contains(email);
	}
	
	public synchronized User register(String email,String password,String nickname) throws Exception{
		if(!checkEmail(email)) {
			throw new Exception("the email has been already existed!");
		}
		User user = newUser();
		_used_email.add(email);
		user.eMail(email);
		user.Password(password);
		_email2users.put(user.eMail(), user);
		_id2users.put(user.ID(), user);
		//Create Attribute On SQL
		UserDAO.Instance().Create(user);
		UserInfoCenter.Instance().create(user, nickname);
		ContactsCenter.Instance().create(user);
		//Logging somthing
		Logger.Instance().logging("New User Joining Us!"
				+ "\n"
				+ "Email:" + email
				+ "\tPassword:" + password);
		//Give to the Domain 
		return user;
	}

	public String Password(String email) {
		return _email2users.get(email).Password();
	}
	public String Password(Integer id) {
		return _id2users.get(id).Password();
	}
	public User UserObject(String email) {
		return _email2users.get(email);
	}
	public User UserObject(Integer id) {
		return _id2users.get(id);
	}
	
	public void ModifyPassword(String email,String password) throws Exception {
		User user = _email2users.get(email);
		if((user != null)) {
			user.Password(password);
			//Update to database
			UserDAO.Instance().Update(user);
		}else {
			throw new Exception("Failed to Modify Users Password!"
					+ "[User of"
					+ email.toUpperCase()
					+ "Does not Existed!]");
		}
	}
	

}
